

(function(win,factory){
	win.Wu = factory();
}(this,(function(){'use strict';
	
var _result = {
	_init:function(){
		
		this.data = {};
	},
	init:function(){
		
		var field = document.getElementById('field').value;
		
		var token = localStorage.getItem(field);
		if(token){
			document.getElementById('value').value = token;
		}
	},
	run:function(id,classname){
		var data = this.data[id];
		
		var self = this;
		try{
			
			var ls_data = eval('('+data.params+')');
			
			var postdata = {};
			if(ls_data){
				var vals = self.get_vals(classname);
				for(var i=0;i<ls_data.length;i++){
					postdata[ls_data[i][0]] = vals[i];
					
				}
			}
			
			var field = document.getElementById('field').value;
			var token = document.getElementById('value').value;
			
			postdata[field] = token;
			localStorage.setItem(field,token);
			
			this.load_data(data.url,postdata,function(result){
				
				console.log(result);
			},function(result){
				console.log(result);
			});
			
		}
		catch(e){
			console.log(e);
		}
		
		//
		
	},
	get_vals(classname){
		var eles = document.getElementsByClassName(classname);
		var arr = [];
		for(var i=0;i<eles.length;i++){
			arr.push(eles[i].value);
		}
		
		return arr;
	},
	setData:function(id,key,val){
		
		var data = this.data[id];
		if(!data){
			data = {};
		}
		
		data[key] = val;
		
		this.data[id] = data;
	},
	load_data:function(url,postdata,success,fail){
		
		this.ajaxPost(url,postdata,function(result){
			
			try{
				var data = eval('('+result+')');
				if(data.result_code==0){
					typeof(success)=='function' && success(data.data);
				}
				else{
					typeof(fail)=='function' && fail(data);
				}
			}
			catch(e){
				console.log(e);
			}
			
		},function(result){
			typeof(fail)=='function' && fail(result);
		});
	},
	ajaxPost:function( url , data , fnSucceed , fnFail , fnLoading ) {
		var ajax = ajaxObject();
		var fnFail = fnFail || '';
		var fnLoading = fnLoading || '';
		var fnSucceed = fnSucceed || '';
		ajax.open( "post" , url , true );
		ajax.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" );
		ajax.onreadystatechange = function () {
			if( ajax.readyState == 4 ) {
				if( ajax.status == 200 ) {
					typeof(fnSucceed)=='function' && fnSucceed( ajax.responseText );
				}
				else {
					typeof(fnFail)=='function' && fnFail( "HTTP请求错误！错误码："+ajax.status );
				}
			}
			else {
				typeof(fnLoading)=='function' && fnLoading();
			}
		}
		var params=formatParams(data);
		
		ajax.send( params );
	 
	}
	
}

//格式化参数
function formatParams(data){
	var arr=[];
	for(var name in data){
		arr.push(encodeURIComponent(name)+'='+encodeURIComponent(data[name]));
	}
	arr.push(('v='+Math.random()).replace('.',''));
	return arr.join('&');
}
function ajaxObject() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
        } 
    catch (e) {
        // Internet Explorer
        try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("您的浏览器不支持AJAX！");
                return false;
            }
        }
    }
    return xmlHttp;
}
	_result._init();
return _result;
})));

